package cc.jq1024.middleware.encrypt.util;


import cc.jq1024.middleware.encrypt.domain.valobj.Algorithm;

import java.security.MessageDigest;


/**
 * 单向加密算法
 * @author li--jiaqiang
 */
public class HASH {

    public static String MD2(String encryptStr) {
        return encrypt(encryptStr, Algorithm.MD2);
    }

    public static String MD5(String encryptStr) {
        return encrypt(encryptStr, Algorithm.MD5);
    }

    public static String SHA1(String encryptStr) {
        return encrypt(encryptStr,Algorithm.SHA1);
    }

    public static String SHA256(String encryptStr) {
        return encrypt(encryptStr,Algorithm.SHA256);
    }

    public static String SHA384(String encryptStr) {
        return encrypt(encryptStr,Algorithm.SHA384);
    }

    public static String SHA512(String encryptStr) {
        return encrypt(encryptStr,Algorithm.SHA512);
    }

    public static String encrypt(String encryptStr, String type) {
        try {
            MessageDigest md = MessageDigest.getInstance(type);
            byte[] mdBytes = md.digest(encryptStr.getBytes());
            return ConvertUtils.byte2HexStr(mdBytes);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}
